package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppFrameComm;
import com.irdstudio.apicenter.console.service.appframe.AppFrameCommService;
import com.irdstudio.apicenter.console.util.FileOperation;
import com.irdstudio.apicenter.console.util.MacmsPathUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 平台模块管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameComm")
public class AppFrameCommAction {
	@Autowired
	private AppFrameCommService appFrameCommService;

	@RequestMapping(value = "appFrameCommDetail1")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		AppFrameComm appFrameComm = appFrameCommService
				.queryAppFrameCommDetail(appid);
		// 防止应用查询设置信息时为无的情况，返回appid
		appFrameComm.setAppId(appid);
		modelAndView.addObject("appFrameComm", appFrameComm);
		modelAndView.setViewName("appframe/comm");
		return modelAndView;
	}

	@RequestMapping(value = "updateAppFrameComm")
	public void updateAppFrameStart(
			@ModelAttribute("appFrameStart") AppFrameComm appFrameComm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();

		int flag = -1;
		String transUrl = "";
		String filePath = "";
		AppFrameComm appFrameComm1 = appFrameCommService
				.queryAppFrameCommDetail(appFrameComm.getAppId());
		if (appFrameComm1.getAppId() == null
				|| "".endsWith(appFrameComm1.getAppId())) {
			flag = appFrameCommService.insertOne(appFrameComm);
			// 保存config.properties
			transUrl = FileOperation.getConfig(appFrameComm.getAppCommUrl());
			filePath = MacmsPathUtil
					.getMacmsAppResPath(appFrameComm.getAppId());
			System.out.println("**********" + transUrl + "&&&&&" + filePath);
			FileOperation.writeFile(filePath, transUrl);
		} else {
			flag = appFrameCommService.updateByKeys(appFrameComm);
			// 保存config.properties
			transUrl = FileOperation.getConfig(appFrameComm.getAppCommUrl());
			filePath = MacmsPathUtil
					.getMacmsAppResPath(appFrameComm.getAppId());
			System.out.println("**********" + transUrl + "&&&&&" + filePath);
			FileOperation.writeFile(filePath, transUrl);
		}
		// int flag = appFrameCommService.updateByKeys(appFrameComm);
		/*
		 * modelAndView.addObject("appFrameComm", appFrameComm);
		 * modelAndView.addObject("result",flag);
		 * modelAndView.setViewName("appframe/comm"); return modelAndView;
		 */

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
